package com.hr.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.hr.common.core.domain.entity.SysUser;
import com.hr.common.utils.uuid.UUID;
import com.hr.system.domain.ClockRule;
import com.hr.system.domain.SysUserRole;
import com.hr.system.mapper.ClockRuleMapper;
import com.hr.system.mapper.SysUserMapper;
import com.hr.system.mapper.SysUserRoleMapper;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hr.system.mapper.SysTenantMapper;
import com.hr.system.domain.SysTenant;
import com.hr.system.service.ISysTenantService;
import com.hr.common.core.text.Convert;

/**
 * 系统租户Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-12
 */
@Service
public class SysTenantServiceImpl implements ISysTenantService 
{
    @Autowired
    private SysTenantMapper sysTenantMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private ClockRuleMapper clockRuleMapper;

    /**
     * 查询系统租户
     * 
     * @param tenantId 系统租户ID
     * @return 系统租户
     */
    @Override
    public SysTenant selectSysTenantById(Long tenantId)
    {
        return sysTenantMapper.selectSysTenantById(tenantId);
    }

    /**
     * 查询系统租户列表
     * 
     * @param sysTenant 系统租户
     * @return 系统租户
     */
    @Override
    public List<SysTenant> selectSysTenantList(SysTenant sysTenant)
    {
        return sysTenantMapper.selectSysTenantList(sysTenant);
    }

    /**
     * 新增系统租户
     * 
     * @param sysTenant 系统租户
     * @return 结果
     */
    @Override
    public int insertSysTenant(SysTenant sysTenant)
    {
        //1、创建租户
        int updateSysTenantCount = sysTenantMapper.insertSysTenant(sysTenant);
        Long tenantId = sysTenant.getTenantId();
        //1、初始化租户管理员账号
        SysUser sysUser = new SysUser();
        sysUser.setTenantId(tenantId.intValue());
        // 英文简称作登录名
        String loginName = sysTenant.getTenantEnIntroduction();
        sysUser.setLoginName(loginName);
        sysUser.setUserName("管理员");
        sysUser.setUserType("00");
        sysUser.setDelFlag("0");
        //随机盐值
        String salt = UUID.randomUUID().toString().substring(0, 5);
        sysUser.setSalt(salt);
        //生成加密密码
        String encryptPassword = new Md5Hash(loginName + "admin123" + salt).toHex();
        sysUser.setPassword(encryptPassword);
        sysUserMapper.insertUser(sysUser);
        //2、初始化角色菜单权限
        Long userId = sysUser.getUserId();
        List<SysUserRole> list = new ArrayList();
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(userId);
        sysUserRole.setRoleId(4L);
        list.add(sysUserRole);
        sysUserRoleMapper.batchUserRole(list);
        //初始化考勤规则
        ClockRule clockRule = clockRuleMapper.selectClockRuleById(1L);
        clockRule.setTenantId(tenantId);
        clockRuleMapper.insertClockRule(clockRule);
        return updateSysTenantCount;
    }

    /**
     * 修改系统租户
     * 
     * @param sysTenant 系统租户
     * @return 结果
     */
    @Override
    public int updateSysTenant(SysTenant sysTenant)
    {
        return sysTenantMapper.updateSysTenant(sysTenant);
    }

    /**
     * 删除系统租户对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSysTenantByIds(String ids)
    {
        return sysTenantMapper.deleteSysTenantByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除系统租户信息
     * 
     * @param tenantId 系统租户ID
     * @return 结果
     */
    @Override
    public int deleteSysTenantById(Long tenantId)
    {
        return sysTenantMapper.deleteSysTenantById(tenantId);
    }

}
